﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Media;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace Base_Complementary_Encryption
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            richTextBox1.Text += "Starting...";
        }

        public enum WindowsAPIKeys
        {
            /// <summary>
            /// 名称
            /// </summary>
            Name,
            /// <summary>
            /// 硬盘容量
            /// </summary>
            Size,
            /// <summary>
            /// 内存容量
            /// </summary>
            Capacity,
            /// <summary>
            /// cpu核心数
            /// </summary>
            NumberOfCores
        }
        public enum WindowsAPIType
        {
            /// <summary>
            /// 内存
            /// </summary>
            Win32_PhysicalMemory,
            /// <summary>
            /// cpu
            /// </summary>
            Win32_Processor,
            /// <summary>
            /// 硬盘
            /// </summary>
            win32_DiskDrive,

        }
        /// <summary>
        /// 获取系统内存大小
        /// </summary>
        /// <returns>内存大小（单位M）</returns>
        public long GetPhisicalMemory()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher();   //用于查询一些如系统信息的管理对象 
            searcher.Query = new SelectQuery(WindowsAPIType.Win32_PhysicalMemory.ToString(), "",
            new string[] { WindowsAPIKeys.Capacity.ToString() });//设置查询条件 
            ManagementObjectCollection collection = searcher.Get();   //获取内存容量 
            ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();

            long capacity = 0;
            while (em.MoveNext())
            {
                ManagementBaseObject baseObj = em.Current;
                if (baseObj.Properties[WindowsAPIKeys.Capacity.ToString()].Value != null)
                {
                    try
                    {
                        capacity += long.Parse(baseObj.Properties[WindowsAPIKeys.Capacity.ToString()].Value.ToString());
                    }
                    catch
                    {
                        return 0;
                    }
                }
            }
            return (capacity / 1000000000);
        }


        /// <summary>
        /// 获取硬盘容量
        /// </summary>
        public string GetDiskSize()
        {
            string result = string.Empty;
            StringBuilder sb = new StringBuilder();
            try
            {
                string hdId = string.Empty;
                ManagementClass hardDisk = new ManagementClass(WindowsAPIType.win32_DiskDrive.ToString());
                ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
                foreach (ManagementObject m in hardDiskC)
                {
                    long capacity = Convert.ToInt64(m[WindowsAPIKeys.Size.ToString()].ToString());
                    sb.Append((capacity/10000000000) + "+");
                }
                result = sb.ToString().TrimEnd('+');
            }
            catch
            {

            }
            return result;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            Random rd = new Random();
            int ri = rd.Next(1, 20);
            if (ri < 10)
            {
                uiAnalogMeter4.Value += rd.Next(1, 5);
              
            }
            else
            {
                uiAnalogMeter4.Value -= rd.Next(1, 5);

            }
            Random rk = new Random();
            int rq = rd.Next(1, 200);
            if (rq == 45 || rq == 16)
            {
                SoundPlayer sp = new SoundPlayer(Application.StartupPath + "\\Res\\kd.wav");
                sp.Play();
                sp = null;
                uiAnalogMeter1.Value -= rd.Next(1, 2);
            }
            if (rq == 23)
            {
                SoundPlayer sp = new SoundPlayer(Application.StartupPath + "\\Res\\cc.wav");
                sp.Play();
                sp = null;
                uiAnalogMeter1.Value += rd.Next(1, 2);
            }
            if (rq == 44)
            {
                uiAnalogMeter2.Value += rd.Next(1, 2);
            }
            if (rq == 74)
            {
                uiAnalogMeter2.Value -= rd.Next(1, 2);
            }
        }

        private void uiLight1_Click(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Random rd = new Random();
            int ri = rd.Next(1, 5);
            if (ri == 1)
            {
                uiSignal1.Level += 1;
            }
            else if(ri == 2)
            {
                uiSignal2.Level += 1;
            }
            else if (ri == 3)
            {
                uiSignal1.Level -= 1;
            }
            else if (ri == 4)
            {
                uiSignal2.Level -= 1;
            }

            if (uiAnalogMeter4.Value <= 400)
            {
                uiAnalogMeter4.Value += 200;
                SoundPlayer sp = new SoundPlayer(Application.StartupPath + "\\Res\\bi.wav");
                sp.Play();
                sp = null;
            }
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            // 原文
            string q = uiRichTextBox1.Text;
            richTextBox1.Text += $"\nSet Query is {q}.";
            // 源语言
            string from = "zh";
            // 目标语言
            string to = "en";
            // 改成您的APP ID
            string appId = "20210729000901164";
            Random rd = new Random();
            string salt = rd.Next(100000).ToString();
            // 改成您的密钥
            string secretKey = "XMr465FulvLqLMMB7iTC";
            string sign = EncryptString(appId + q + salt + secretKey);
            string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
            url += "q=" + HttpUtility.UrlEncode(q);
            url += "&from=" + from;
            url += "&to=" + to;
            url += "&appid=" + appId;
            url += "&salt=" + salt;
            url += "&sign=" + sign;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = 6000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            uiRichTextBox1.Text = 
                retString.Split(
                    new string[] { "\"dst\":" }, 
                    StringSplitOptions.None)[1].Split
                    (new string[] { "\"}]}" },StringSplitOptions.None)[0].Split('\"')[1].ToLower()
                    ;
            richTextBox1.Text += $"\nGet {uiRichTextBox1.Text}.";
            //进行算法
            //获取每一个字母，并按照单词存储
            List<string> kdt = new List<string>();
            kdt.Add("AUG");
            richTextBox1.Text += "\n安装起始密码子AUG.";
            foreach (var item in uiRichTextBox1.Text)
            {
                string thisLetter = "";
                richTextBox1.Text += $"\n拼接密码子来自{(int)item}.";
                if (item == ' ')
                {
                    Console.WriteLine((int)item);
                }
                else
                {
                    Console.WriteLine((int)item);
                    if (item <= 102)
                    {
                        thisLetter += (BaseEnums.Leu)(new Random()).Next(0,6);
                        switch ((int)item)
                        {
                            case 97:
                                thisLetter += (BaseEnums.Ala)(new Random()).Next(0, 4);
                                break;
                            case 98:
                                thisLetter += (BaseEnums.Tyr)(new Random()).Next(0, 2);
                                break;
                            case 99:
                                thisLetter += (BaseEnums.Glu)(new Random()).Next(0, 4);
                                break;
                            case 100:
                                thisLetter += (BaseEnums.Asn)(new Random()).Next(0, 4);
                                break;
                            case 101:
                                thisLetter += (BaseEnums.Arg)(new Random()).Next(0, 6);
                                break;
                            case 102:
                                thisLetter += (BaseEnums.Gly)(new Random()).Next(0, 4);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (item <= 108)
                    {
                        thisLetter += (BaseEnums.Met)(new Random()).Next(0, 5);
                        switch ((int)item)
                        {
                            case 103:
                                thisLetter += (BaseEnums.Ala)(new Random()).Next(0, 4);
                                break;
                            case 104:
                                thisLetter += (BaseEnums.Tyr)(new Random()).Next(0, 2);
                                break;
                            case 105:
                                thisLetter += (BaseEnums.Glu)(new Random()).Next(0, 4);
                                break;
                            case 106:
                                thisLetter += (BaseEnums.Asn)(new Random()).Next(0, 4);
                                break;
                            case 107:
                                thisLetter += (BaseEnums.Arg)(new Random()).Next(0, 6);
                                break;
                            case 108:
                                thisLetter += (BaseEnums.Gly)(new Random()).Next(0, 4);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (item <= 114)
                    {
                        thisLetter += (BaseEnums.Val)(new Random()).Next(0, 5);
                        switch ((int)item)
                        {
                            case 109:
                                thisLetter += (BaseEnums.Ala)(new Random()).Next(0, 4);
                                break;
                            case 110:
                                thisLetter += (BaseEnums.Tyr)(new Random()).Next(0, 2);
                                break;
                            case 111:
                                thisLetter += (BaseEnums.Glu)(new Random()).Next(0, 4);
                                break;
                            case 112:
                                thisLetter += (BaseEnums.Asn)(new Random()).Next(0, 4);
                                break;
                            case 113:
                                thisLetter += (BaseEnums.Arg)(new Random()).Next(0, 6);
                                break;
                            case 114:
                                thisLetter += (BaseEnums.Gly)(new Random()).Next(0, 4);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (item <= 120)
                    {
                        thisLetter += (BaseEnums.Ser)(new Random()).Next(0, 7);
                        switch ((int)item)
                        {
                            case 115:
                                thisLetter += (BaseEnums.Ala)(new Random()).Next(0, 4);
                                break;
                            case 116:
                                thisLetter += (BaseEnums.Tyr)(new Random()).Next(0, 2);
                                break;
                            case 117:
                                thisLetter += (BaseEnums.Glu)(new Random()).Next(0, 4);
                                break;
                            case 118:
                                thisLetter += (BaseEnums.Asn)(new Random()).Next(0, 4);
                                break;
                            case 119:
                                thisLetter += (BaseEnums.Arg)(new Random()).Next(0, 6);
                                break;
                            case 120:
                                thisLetter += (BaseEnums.Gly)(new Random()).Next(0, 4);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (item <= 122)
                    {
                        thisLetter += (BaseEnums.Pro)(new Random()).Next(0, 4);
                        switch ((int)item)
                        {
                            case 121:
                                thisLetter += (BaseEnums.Ala)(new Random()).Next(0, 4);
                                break;
                            case 122:
                                thisLetter += (BaseEnums.Tyr)(new Random()).Next(0, 2);
                                break;
                            default:
                                break;
                        }
                    }
                }
                kdt.Add(thisLetter);
                thisLetter = "";
            }
            kdt.Add("UGA");
            richTextBox1.Text += "\n安装终止密码子UGA.";
            uiRichTextBox1.Text = "";
            int i = 0;
            foreach (var item in kdt)
            {
                if (i == 0)
                {
                    uiRichTextBox1.Text += item;
                }
                else if (i == kdt.Count)
                {
                    uiRichTextBox1.Text += item;
                }
                else
                {
                    item.Replace('A', 'U');
                    item.Replace('U', 'A');
                    item.Replace('C', 'G');
                    item.Replace('G', 'C');
                    uiRichTextBox1.Text += item;
                }
            }
            richTextBox1.Text += "\n合成互补RNA.";
            kdt.Clear();
        }
        // 计算MD5值
        public static string EncryptString(string str)
        {
            MD5 md5 = MD5.Create();
            // 将字符串转换成字节数组
            byte[] byteOld = Encoding.UTF8.GetBytes(str);
            // 调用加密方法
            byte[] byteNew = md5.ComputeHash(byteOld);
            // 将加密结果转换为字符串
            StringBuilder sb = new StringBuilder();
            foreach (byte b in byteNew)
            {
                // 将字节转换成16进制表示的字符串，
                sb.Append(b.ToString("x2"));
            }
            // 返回加密的字符串
            return sb.ToString();
        }
        /// <summary>
        /// MD5计算
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns></returns>
        public static string GetMd5Hash(String input)
        {
            if (input == null)
            {
                return null;
            }
            MD5 md5Hash = MD5.Create();
            // 将输入字符串转换为字节数组并计算哈希数据
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            // 创建一个 Stringbuilder 来收集字节并创建字符串
            StringBuilder sBuilder = new StringBuilder();
            // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // 返回十六进制字符串
            return sBuilder.ToString();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            uiAnalogMeter1.Value = GetPhisicalMemory();
            uiAnalogMeter2.Value = Convert.ToInt32(GetDiskSize());
        }
    }
}
